﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ERPStore.Extensions;

namespace ERPStore.Demo.EventConsumers
{
	public class ApplicationStartedEventConsumer : EventBrokR.IConsumer<ERPStore.Events.ApplicationStartedEvent>
	{
		public ApplicationStartedEventConsumer(ERPStore.Offers.Services.AdminOffersService adminOffersService,
			ERPStore.Services.ICatalogService catalogService)
		{
			AdminOffersService = adminOffersService;
			CatalogService = catalogService;
		}

		protected ERPStore.Offers.Services.AdminOffersService AdminOffersService { get; private set; }
		protected ERPStore.Services.ICatalogService CatalogService { get; private set; }

		public void Handle(ERPStore.Events.ApplicationStartedEvent eventMessage)
		{
			GlobalConfiguration.Configuration.Settings.CountryId = ERPStore.Models.Country.USA.Id;
			GlobalConfiguration.Configuration.Settings.LanguageId = ERPStore.Models.Language.English.Id;

			CreateHomeOffer();
			CreateSpecialOffer();

			AdminOffersService.RefreshAllOffers();

			CreateFakeCoupon();
		}

		private void CreateHomeOffer()
		{
			var offer = AdminOffersService.CreateOffer();

			offer.Name = "TheBigOffer";
			offer.Link = "offer_of_the_year";
			offer.Description = "This is the best offer now for you incredible prices";
			offer.EndDate = DateTime.Now.AddDays(10);
			offer.Title = "The offer";

			AdminOffersService.SaveOffer(offer);

			var product = CatalogService.GetProductById(1);

			var offeredProduct = AdminOffersService.CreateOfferedProduct();
			offeredProduct.ProductId = 1;
			offeredProduct.SalePrice = product.SalePrice.Value - 10;
			offeredProduct.OfferId = offer.Id;

			AdminOffersService.SaveOfferedProduct(offeredProduct);

			offeredProduct = AdminOffersService.CreateOfferedProduct();
			product = CatalogService.GetProductById(10);

			offeredProduct.ProductId = 10;
			offeredProduct.SalePrice = product.SalePrice.Value - 15;
			offeredProduct.OfferId = offer.Id;

			AdminOffersService.SaveOfferedProduct(offeredProduct);

			offeredProduct = AdminOffersService.CreateOfferedProduct();
			product = CatalogService.GetProductById(11);

			offeredProduct.ProductId = 11;
			offeredProduct.SalePrice = product.SalePrice.Value - 5;
			offeredProduct.OfferId = offer.Id;

			AdminOffersService.SaveOfferedProduct(offeredProduct);
		}

		private void CreateSpecialOffer()
		{
			var offer = AdminOffersService.CreateOffer();

			offer.Name = "TheSpecialOffer";
			offer.Link = "special_offer_camera";
			offer.Description = "This is the best offer for a large choice of cameras";
			offer.EndDate = DateTime.Now.AddDays(10);
			offer.Title = "The camera offer";

			AdminOffersService.SaveOffer(offer);

			var product = CatalogService.GetProductByCode("B004J3V90Y");

			var offeredProduct = AdminOffersService.CreateOfferedProduct();
			offeredProduct.ProductId = product.Id;
			offeredProduct.SalePrice = product.SalePrice.Value - 20;
			offeredProduct.OfferId = offer.Id;

			AdminOffersService.SaveOfferedProduct(offeredProduct);
		}

		private void CreateFakeCoupon()
		{
			var incentiveService = ERPStore.GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Coupons.Services.AdminIncentiveService>();
			var coupon = incentiveService.CreateCoupon();

			coupon.Code = "FakeCoupon";
			coupon.ProviderName = "globaldiscount";
			coupon.DiscountPercent = 0.2d;
			coupon.Description = "20% discount !";
			var expirationDate = DateTime.Now.AddDays(10);
			coupon.ExpirationDate = expirationDate;

			incentiveService.SaveCoupon(coupon);
		}

	}
}
